package com.ruoyi.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.FileUpload;
import com.ruoyi.system.domain.dto.FileUploadDTO;
import com.ruoyi.system.domain.vo.FileUploadVO;
import com.ruoyi.system.mapper.FileUploadMapper;
import com.ruoyi.system.service.FileUploadService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FileUploadServiceImpl extends ServiceImpl<FileUploadMapper, FileUpload> implements FileUploadService {

    @Resource
    private FileUploadMapper fileUploadMapper;

    @Override
    public Long addFileUpload(FileUploadDTO dto) {
        FileUpload fileUpload = new FileUpload();
        BeanUtil.copyProperties(dto, fileUpload);
        fileUploadMapper.insert(fileUpload);
        return fileUpload.getId();
    }

    @Override
    public void updateFileUpload(FileUploadDTO dto) {
        if (dto.getId() == null) {
            throw new IllegalArgumentException("文件ID不能为空");
        }
        FileUpload fileUpload = getById(dto.getId());
        if (fileUpload == null) {
            throw new IllegalArgumentException("文件记录不存在");
        }
        BeanUtil.copyProperties(dto, fileUpload, "id", "createdAt");
        updateById(fileUpload);
    }

    @Override
    @Transactional
    public void deleteFileUpload(Long id) {
        FileUpload fileUpload = getById(id);
        if (fileUpload == null) {
            throw new IllegalArgumentException("文件记录不存在");
        }
//        fileUpload.setStatus("deleted");
//        updateById(fileUpload);
        fileUploadMapper.deleteById(id);
    }

    @Override
    public FileUploadVO getFileUploadById(Long id) {
        FileUpload fileUpload = getById(id);
        if (fileUpload == null) {
            return null;
        }
       FileUploadVO vo = new FileUploadVO();
        BeanUtil.copyProperties(fileUpload, vo);
        return vo;
    }

    @Override
    public List<FileUploadVO> listFileUploads(String businessType, String businessId, String fileType, String status) {
        LambdaQueryWrapper<FileUpload> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(businessType)) {
            wrapper.eq(FileUpload::getBusinessType, businessType);
        }
        if (StrUtil.isNotBlank(businessId)) {
            wrapper.eq(FileUpload::getBusinessId, businessId);
        }
        if (StrUtil.isNotBlank(fileType)) {
            wrapper.eq(FileUpload::getFileType, fileType);
        }
        if (StrUtil.isNotBlank(status)) {
            wrapper.eq(FileUpload::getStatus, status);
        }
        wrapper.ne(FileUpload::getStatus, "deleted"); // 排除已删除记录
        wrapper.orderByDesc(FileUpload::getUploadTime);

        List<FileUpload> fileUploads = list(wrapper);
        return fileUploads.stream()
                .map(fileUpload -> {
                  FileUploadVO vo = new FileUploadVO();
                    BeanUtil.copyProperties(fileUpload, vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<FileUploadVO> listFileUploadsByBusinessId(String businessId) {
        LambdaQueryWrapper<FileUpload> queryWrapper = new LambdaQueryWrapper<>();
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            queryWrapper.eq(FileUpload::getUploadedBy, userId.toString());
        }
        queryWrapper.eq(FileUpload::getBusinessId, businessId);
        List<FileUpload> fileList = list(queryWrapper);
        // 转换为 FileUploadVO
        List<FileUploadVO> voList = new ArrayList<>();
        for (FileUpload file : fileList) {
            FileUploadVO vo = new FileUploadVO();
            BeanUtils.copyProperties(file, vo);
            vo.setUrl(file.getFilePath()); // filePath 作为 URL
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<FileUploadVO> listFileUploadsByBusinessIds(List<String> businessIds) {
        if (businessIds == null || businessIds.isEmpty()) {
            return new ArrayList<>();
        }

        // 构建查询条件
        LambdaQueryWrapper<FileUpload> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FileUpload::getBusinessId, businessIds);

        // 查询 file_upload 表
        List<FileUpload> fileList = list(queryWrapper);

        // 转换为 FileUploadVO
        List<FileUploadVO> voList = new ArrayList<>();
        for (FileUpload file : fileList) {
            FileUploadVO vo = new FileUploadVO();
            BeanUtils.copyProperties(file, vo);
            vo.setUrl(file.getFilePath()); // filePath 作为 URL
            voList.add(vo);
        }

        return voList;
    }
}
